package com.hnevc.mukewangapp;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment{

    List<Fragment> fragmentList;
    ViewPager mViewPager;
    RadioGroup radioGroup;


    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        mViewPager = view.findViewById(R.id.home_container);
        radioGroup = view.findViewById(R.id.rg_btn_menu);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 在 activity 创建完成后 执行
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment1());
        fragmentList.add(new HomeFragment2());
        fragmentList.add(new HomeFragment3());
        mViewPager.setAdapter(new MyFragmentAdapter(getChildFragmentManager()));

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position){
                    case 0:
                        radioGroup.check(R.id.btn_tj);
                        break;
                    case 1:
                        radioGroup.check(R.id.btn_free);
                        break;
                    case 2:
                        radioGroup.check(R.id.btn_szk);
                        break;
                }

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.btn_tj:
                       if (mViewPager.getCurrentItem()!=0){
                           mViewPager.setCurrentItem(0,false);
                       }
                       break;
                    case R.id.btn_free:
                        if (mViewPager.getCurrentItem()!=1){
                            mViewPager.setCurrentItem(1,false);
                        }
                        break;
                    case R.id.btn_szk:
                        if (mViewPager.getCurrentItem()!=2){
                            mViewPager.setCurrentItem(2,false);
                        }
                        break;
                }
            }
        });
    }
    class MyFragmentAdapter extends FragmentPagerAdapter{


        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }



}
